package com.aifast.system.sys.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;

import com.aifast.common.cache.util.RedisUtil;
import com.aifast.common.layui.LayuiPageFactory;
import com.aifast.system.sys.entity.SysRedis;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author fujian
 * @since 2022/3/28 17:31
 */
public class SysRedisService {

    public static Object list(String searchValue, Page page) {
        if (StrUtil.isBlank(searchValue)) {
            searchValue = "*";
        }
        Set<String> keys = RedisUtil.keys(searchValue);
        if (CollUtil.isEmpty(keys)) {
            return null;
        }
        // 设置页码
        int pageNo = Convert.toInt(page.getCurrent());
        int pageSize = Convert.toInt(page.getSize());
        int start = pageNo == 1 ? 0 : (pageNo - 1) * pageSize;
        List<String> keyList = new ArrayList<>(keys);
        List<SysRedis> list = new ArrayList<SysRedis>();
        for (int i = start, j = 0; i < keyList.size() && j < pageSize; i++, j++) {
            String key = keyList.get(i);
            SysRedis base = baseInfo(key);
            list.add(base);
        }

        page.setRecords(list);
        page.setTotal(keyList.size());
        return LayuiPageFactory.init(page);
    }

    public static SysRedis baseInfo(String key) {
        RedisTemplate<String, Object> template = RedisUtil.getTemplate();
        DataType type = RedisUtil.type(key);
        Long expire = RedisUtil.getExpire(key);
        SysRedis sysRedis = SysRedis.builder().type(type.code()).key(key).expire(convertTimeStr(expire)).build();
        switch (type) {
            case STRING:
                sysRedis.setValStr(RedisUtil.get(key));
                sysRedis.setElmNum(1L);
                break;
            case LIST:
                sysRedis.setValList(RedisUtil.lrange(key,0,-1));
                sysRedis.setElmNum(template.opsForList().size(key));
                break;
            case SET:
                sysRedis.setValSet(RedisUtil.smembers(key));
                sysRedis.setElmNum(template.opsForSet().size(key));
                break;
            case HASH:
                sysRedis.setValMap(RedisUtil.hmget(key));
                sysRedis.setElmNum(template.opsForHash().size(key));
                break;
            default:
                break;
        }
        return sysRedis;
    }

    public static SysRedis get(String key){
        RedisTemplate<String, Object> template = RedisUtil.getTemplate();
        DataType type = RedisUtil.type(key);
        Long expire = RedisUtil.getExpire(key);
        SysRedis sysRedis = SysRedis.builder().type(type.code()).key(key).expire(String.valueOf(expire)).build();
        switch (type) {
            case STRING:
                Object redisValue = template.opsForValue().get(key);
                sysRedis.setValStr(String.valueOf(redisValue));
                sysRedis.setElmNum(1L);
                break;
            case LIST:
                List<Object> list = template.opsForList().range(key, 0, -1);
                sysRedis.setValList(list);
                sysRedis.setElmNum(template.opsForList().size(key));
                break;
            case SET:
                Set<Object> set = template.opsForSet().members(key);
                sysRedis.setValSet(set);
                sysRedis.setElmNum(template.opsForSet().size(key));
                break;

            case HASH:
                Map<Object, Object> map = RedisUtil.hmget(key);
                sysRedis.setValMap(map);
                sysRedis.setElmNum(template.opsForHash().size(key));
                break;
            default:
                break;
        }
        return sysRedis;
    }

    /**
     * 时间（秒）转成中文时间
     */
    private static String convertTimeStr(Long expire) {

        if (expire < 0) {
            return "永久";
        }

        long day = expire / 60 / 60 / 24;
        long hour = expire / 60 / 60 % 24;
        long minute = expire / 60 % 60;
        long seconds = expire % 60;

        StringBuilder sb = new StringBuilder();
        if (day != 0) {
            sb.append(day).append("天");
        }
        if (hour != 0) {
            sb.append(hour).append("小时");
        }
        if (minute != 0) {
            sb.append(minute).append("分");
        }
        if (seconds != 0) {
            sb.append(seconds).append("秒");
        }
        return sb.toString();
    }

}
